package jp.co.piratealliance.pirateAllianceWeb.service.task.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jp.co.piratealliance.pirateAllianceWeb.dao.task.TaskDao;
import jp.co.piratealliance.pirateAllianceWeb.def.Constants;
import jp.co.piratealliance.pirateAllianceWeb.dto.goods.ResourceTypeInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.OrderInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.task.ScheduleAndEventInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.task.TaskFeeDetailInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.task.TaskInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.CommonHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.task.TaskService;


/**
*
* copyright   pirate alliance team 2014
* @author     lw
*
*/

public class TaskServiceImpl implements TaskService {

	private TaskDao taskDao;

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	//任務登録画面に、該当ユーザーの全部任務情報を検索する
	public List<TaskInfoDto> getTaskListByUserId(String user_id){
		return taskDao.selectTaskListByUserId(user_id);
	}
	
	public List<ScheduleAndEventInfoDto> getEventListByUserId(String order_id){
		return taskDao.selectEventListByOrderId(order_id);
	}
	
	//新增日程信息
	public void insertEventInfo(ScheduleAndEventInfoDto eventInfo) {
		String eventIdSeq = CommonHelper.getSequence(Constants.TB_EVENT);
		String scheduleIdSeq = CommonHelper.getSequence(Constants.TB_SCHEDULE);
		
		eventInfo.setEvent_id(eventIdSeq);//日程Id
		eventInfo.setSchedule_id(scheduleIdSeq);//任务Id
		
		taskDao.insertScheduleInfo(eventInfo);//插入任务表
		taskDao.insertEventInfo(eventInfo);//插入日程表
		
	}
	
	//更新日程信息
	public boolean updateEventInfo(ScheduleAndEventInfoDto eventInfo){
		boolean successFlag=false;
		boolean insertSFlag = taskDao.updateScheduleInfo(eventInfo);//更新任务表
		boolean insertEFlag = taskDao.updateEventInfo(eventInfo);//更新日程表
		if(insertSFlag && insertEFlag){
			successFlag=true;
		}
		return successFlag;
	}

	
	//查询日程信息
	public List<ScheduleAndEventInfoDto> getEventListByEventId(
				Map<String, Object> param){
		return taskDao.selectEventListByEventId(param);
		}
	
	//删除日程信息
	public void deleteEventListByEventId(Map<String, Object> param){
		taskDao.deleteEventListByEventId(param);
		taskDao.deleteScheduleListByScheduleId(param);
	}
	
	//选定的任务基本信息取得
	public TaskInfoDto getTaskInfoByOrderId(String order_id){
		TaskInfoDto taskInfo = new TaskInfoDto();
		 taskInfo = taskDao.selectTaskInfoByOrderId(order_id);
		 if(taskInfo==null){
			 return null;
		 }
		 
		 //サービス要望の資源商品分類
		 String resourceTypeIds = taskInfo.getResource_type_ids();
		 List<ResourceTypeInfoDto> resourceTypeInfoList = new ArrayList<ResourceTypeInfoDto>();
		 if(resourceTypeIds!=null){
			 String[] resourceTypeIdArray = resourceTypeIds.split(",");
			 for(String resourceTypeId : resourceTypeIdArray){
				 ResourceTypeInfoDto resourceTypeInfoDto = taskDao.selectResourceTypeInfo(resourceTypeId);
				 if(resourceTypeInfoDto!=null){
					 resourceTypeInfoList.add(resourceTypeInfoDto);
				 }
			 }
		 }
		 
		 //设定要望资源
		 taskInfo.setResourceTypeInfoList(resourceTypeInfoList);
		 
		 return taskInfo;
	}
	
	//选定的任务,费用明细信息取得
	public List<TaskFeeDetailInfoDto> getFeeDetailListByOrderId(String order_id){
		return taskDao.selectFeeDetailListByOrderId(order_id);
	}
	
	//设定任务信息
	public void saveTaskInfo(OrderInfoDto orderInfo){
		taskDao.updateOrder(orderInfo);
	}
}
